//
//  RVersionUpdateView.m
//  ClientTax
//
//  Created by 侯伟 on 2020/6/24.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "RVersionUpdateView.h"
//#import "RSThemeButton.h"
#import "UIView+Border.h"
#import "RVersionCheckRequest.h"

@interface RVersionUpdateView ()

@property(nonatomic,weak)MyLinearLayout* contentLayout;
@property(nonatomic,weak)MyLinearLayout* bottomtLayout;

/// 标题
@property(nonatomic,weak)UILabel* titleLb;

/// 内容
@property(nonatomic,weak)UILabel* contentLb;

///// 跳过
//@property(nonatomic,weak)UIButton* skipBtn;
//

@end

@implementation RVersionUpdateView

+ (instancetype)share{
    static RVersionUpdateView* versionView = NULL;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        versionView = [[RVersionUpdateView alloc] initWithFrame:SCREEN_BOUNDS];
    });
    return versionView;
}

- (instancetype)init{
    if(self = [super init]){
        [self initSubViews];
        
        self.backgroundColor = RGBACOLOR(0x33, 0x33, 0x33, 0.5);
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self initSubViews];
        
        self.backgroundColor = RGBACOLOR(0x00, 0x00, 0x00, 0.5);
        
    }
    return self;
}

- (void)initSubViews{
    
    //内容视图
    MyLinearLayout* contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    contentLayout.backgroundColor = UIColor.whiteColor;
    contentLayout.layer.cornerRadius = PX(10);
    contentLayout.layer.masksToBounds = YES;
    contentLayout.myCenterY = -44;
    contentLayout.myCenterX = 0;
    contentLayout.myWidth = PX(300);
    contentLayout.myHeight = MyLayoutSize.wrap;
    [self addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    //顶部背景
    MyLinearLayout* topLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    topLayout.myMargin = 0;
    topLayout.padding = UIEdgeInsetsMake(32, 10, 32, 10);
    topLayout.subviewHSpace = 10;
    topLayout.heightSize.equalTo(@(MyLayoutSize.wrap)).min(PX(128));
    [contentLayout addSubview:topLayout];
    
    //背景
    UIImageView* imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"x_version_update"]];
    imageView.myCenterY = imageView.myLeft = 0;
    [imageView sizeToFit];
    [topLayout addSubview:imageView];
    
    
    MyLinearLayout* textLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Vert)];
    textLayout.myMargin = 0;
    textLayout.subviewVSpace = 20;
    textLayout.myHeight = MyLayoutSize.wrap;
    textLayout.weight = 1.0;
    [topLayout addSubview:textLayout];
    
    //title
    UILabel* titleLb = [UILabel lableWithFont:FONTBOLD_FIT(18) color:COLOR_THEME_A text:@"发现新版本\nV1.0"];
    titleLb.numberOfLines = 2;
    titleLb.myHeight = MyLayoutSize.wrap;
    titleLb.myHorzMargin = 0;
    [titleLb sizeToFit];
    [textLayout addSubview:titleLb];
    _titleLb = titleLb;
    
    
    //更新内容
    UILabel* contentLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_GRAY_BASE text:NULL];
    contentLb.numberOfLines = 0;
    contentLb.myHeight = MyLayoutSize.wrap;
    contentLb.myHorzMargin = 0;
    [contentLb sizeToFit];
    [textLayout addSubview:contentLb];
    _contentLb = contentLb;
    
    
    //更新
    UIButton* updateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [updateBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [updateBtn setTitle:@"立即更新" forState:UIControlStateNormal];
    [updateBtn addTarget:self action:@selector(update:) forControlEvents:UIControlEventTouchUpInside];
    updateBtn.titleLabel.font = FONT_FIT(15);
    updateBtn.backgroundColor = COLOR_THEME_A;
    [updateBtn setShadow:RGBACOLOR(0xFF, 0xD8, 0xB8, 1.0) offset:CGSizeMake(0,1) opacity:1 radius:8];
    updateBtn.myHeight = PX(44);
    updateBtn.myHorzMargin = 0;
    updateBtn.myTop = 0;
    [contentLayout addSubview:updateBtn];
    
//    //跳过更新
//    UIButton* skipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [skipBtn setTitleColor:COLOR_GRAY_LIGHT forState:UIControlStateNormal];
//    [skipBtn setTitle:@"残忍拒绝" forState:UIControlStateNormal];
//    [skipBtn addTarget:self action:@selector(skip:) forControlEvents:UIControlEventTouchUpInside];
//    skipBtn.titleLabel.font = FONT_FIT(15);
//    skipBtn.myHeight = PX(40);
//    skipBtn.myHorzMargin = 0;
//    [bottomLayout addSubview:skipBtn];
//    _skipBtn = skipBtn;
}

#pragma mark  -- setter

- (void)setUpdateInfo:(RUpdateInfo *)updateInfo{
    _updateInfo = updateInfo;
    
    self.titleLb.text = [NSString stringWithFormat:@"发现新版本\nV%@",updateInfo.version];
    [self.titleLb sizeToFit];
    
    if(updateInfo.content.length){
        
        NSString* content = updateInfo.content;
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:8.0];
        
        NSDictionary* sttributes = @{NSFontAttributeName:FONT_FIT(15),
                                      NSForegroundColorAttributeName:COLOR_GRAY_BASE,
                                      NSParagraphStyleAttributeName:paragraphStyle};
        
        NSMutableAttributedString * contentText = [[NSMutableAttributedString alloc] initWithData:[content dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes: NULL error:nil];
        [contentText setAttributes:sttributes range:NSMakeRange(0, contentText.mutableString.length)];
        
        self.contentLb.attributedText = contentText;
        [self.contentLb sizeToFit];
    }
}

#pragma mark --

- (void)update:(UIButton*)sender{

    [self animationWithFlag:NO complete:^{
        [self removeFromSuperview];
        [Founction openURLString:APP_STORE_URL];
    }];
}

- (void)skip:(UIButton*)sender{
    
    [self animationWithFlag:NO complete:^{
        [self removeFromSuperview];
    }];
}

#pragma mark  -- animation

- (void)didMoveToSuperview{
    [super didMoveToSuperview];
    if(self.superview){
        self.contentLayout.transform = CGAffineTransformMakeScale(0, 0);
        self.alpha = 0.000001;
        [self animationWithFlag:YES complete:NULL];
    }
}

- (void)animationWithFlag:(BOOL)flag complete:(VoidBlock _Nullable)complete{
    
    CGFloat transform = flag?1:0.000001;
    CGFloat alpha = flag?1.0:0.000001;
    
    [UIView animateWithDuration:0.25 animations:^{
        self.contentLayout.transform = CGAffineTransformMakeScale(transform, transform);
        self.alpha = alpha;
    } completion:^(BOOL finished) {
        if(complete){
            complete();
        }
    }];
}
@end
